// 用户小程序  
import $http from "@/ajax/http.js";

export const index_users = {
	data() {
		return {
			margin: '',
			roles: "",
			token: '',
			status: '',
			yuyue: '预约取液'
		};
	},
	onLoad(options) {
		this.token = wx.getStorageSync('token');
		this.roles = wx.getStorageSync('roles');
		console.log(this.roles, "为啥获取不到");
		this.init();
	},
	onShow() {
		this.status = wx.getStorageSync('status')
		if (this.status === 1) {
			this.yuyue = "预约成功"
		} else if (this.status === 0) {
			this.yuyue = "预约取液"
		}
	},
	methods: {
		init() {
			// 获取取液次数
			$http.request({
				header: {
					Authorization: this.token,
				},
				url: '/storeuser/index/getLeftWaterTimes',
				method: 'GET',
			}).then((res) => {
				this.margin = res.margin
				console.log(res, "获取取液次数");
			});
		},

		appoint() {
			// 点击预约取液
			$http.request({
				header: {
					Authorization: this.token,
				},
				url: '/storeuser/index/orderWater',
				method: 'POST',
			}).then((res) => {
				console.log(res, "点击预约取液");
				if (res.code === 500) {
					wx.showModal({
						title: '提示',
						content: res.message,
						showCancel: false
					})
				} else if (res.currReservationStatus === 0) {
					wx.setStorageSync("status", res.currReservationStatus)
					wx.showModal({
						title: '提示',
						content: '您已取消预约！',
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								this.yuyue = "预约取液"
							}
						}
					})
				} else if (res.currReservationStatus === 1) {
					wx.setStorageSync("status", res.currReservationStatus)
					wx.showModal({
						title: '提示',
						content: '您已预约成功！',
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								this.yuyue = "预约成功"
							}
						}
					})
				}
			});
		}
	},
};